package quota

import (
	"fmt"

	bm "go-main/library/net/http/dam"
	"go-main/library/net/metadata"
)

type Rule interface {
	Name() string
	Key(c *bm.Context) string
}

type RuleTotal struct{}

func (r *RuleTotal) Name() string {
	return "total"
}

func (r *RuleTotal) Key(c *bm.Context) string {
	return "total"
}

type RuleCaller struct{}

func (r *RuleCaller) Name() string {
	return "caller"
}

func (r *RuleCaller) Key(c *bm.Context) string {
	caller := metadata.String(c, metadata.Caller)
	if caller == "" {
		caller = "blank"
	}
	return fmt.Sprintf("caller:%s", caller)
}

type RuleAppkey struct{}

func (r *RuleAppkey) Name() string {
	return "appkey"
}

func (r *RuleAppkey) Key(c *bm.Context) string {
	appkey := c.Request.Form.Get("appkey")
	if appkey == "" {
		appkey = "blank"
	}
	return fmt.Sprintf("appkey:%s", appkey)
}
